package com.qf.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qf.pojo.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/7/6 13:27
 */
@RestController
@RequestMapping("/search")
public class SearchController {

    @RequestMapping("/test")
    public String test(){
        return "这是一个search微服务,被调用了";
    }

    @RequestMapping("/query/{id}")
    @HystrixCommand(fallbackMethod = "findUserByIdFallBack")
    public User findUserById(@PathVariable("id") Integer id){
        System.out.println(id);
        //模拟远程调用出现异常
        int i = 1/0;
        User user = new User();
        user.setName("詹野种");
        user.setId(id);
        user.setBirthday(new Date());

        return user;
    }

    /**
     * 兜底方法
     */
    public User findUserByIdFallBack(Integer id){
        User user = new User();
        user.setId(0);
        user.setName("詹野种,滚出去");

        return user;
    }
}
